package com.haisen.service.impl;

import com.github.pagehelper.PageHelper;
import com.haisen.core.utils.PageResultSet;
import com.haisen.dataobject.entity.JobSchedule;
import com.haisen.dataobject.mapper.base.JobScheduleMapper;
import com.haisen.dataobject.query.JobScheduleQuery;
import com.haisen.service.JobScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class JobScheduleServiceImp implements JobScheduleService {

    @Autowired
    private JobScheduleMapper jobScheduleMapper;

    @Override
    public void create(JobSchedule jobSchedule) {
        if (selectOneByUsername(jobSchedule.getUsername()) == null){
            jobScheduleMapper.insertSelective(jobSchedule);
        }else{
            jobScheduleMapper.updateByPrimaryKeySelective(jobSchedule);
        }
    }

    @Override
    public void update(JobSchedule jobSchedule) {
        jobScheduleMapper.updateByPrimaryKeySelective(jobSchedule);
    }

    @Override
    public void delete(Long[] ids) {
        Weekend<JobSchedule> example = Weekend.of(JobSchedule.class);
        WeekendCriteria<JobSchedule,Object> criteria = example.weekendCriteria();
        criteria.andIn(JobSchedule::getId, Arrays.asList(ids));
        jobScheduleMapper.deleteByExample(example);
    }

   public JobSchedule selectOneByUsername(String username){
        Weekend<JobSchedule> example = Weekend.of(JobSchedule.class);
        WeekendCriteria<JobSchedule,Object> criteria = example.weekendCriteria();
        criteria.andEqualTo(JobSchedule::getUsername,username);
        return jobScheduleMapper.selectOneByExample(example);
   }

    @Override
    public List<String> listUsernameByJobNoId(Long jobNoId) {
        List<JobSchedule> jobScheduleList = jobScheduleMapper.selectAll();
       return  jobScheduleList.stream().filter( o ->{
            if (o.getJobNoList().contains(jobNoId)) return true;
            else return false;
        }).map( o-> {
            return o.getUsername();
        }).collect(Collectors.toList());
    }

    @Override
    public PageResultSet<JobSchedule> findByPage(JobScheduleQuery jobScheduleQuery) {
        /**
         * 排序
         */
        if (!StringUtils.isEmpty(jobScheduleQuery.getOrder())){
            PageHelper.orderBy(jobScheduleQuery.getOrderBy());
        }
        PageHelper.offsetPage(jobScheduleQuery.getOffset(),jobScheduleQuery.getLimit());
        Weekend<JobSchedule> example = Weekend.of(JobSchedule.class);
        WeekendCriteria<JobSchedule,Object> criteria = example.weekendCriteria();
        if (!StringUtils.isEmpty(jobScheduleQuery.getCdate())){
            criteria.andEqualTo(JobSchedule::getCdate,jobScheduleQuery.getCdate());
        }
        if (!StringUtils.isEmpty(jobScheduleQuery.getUsername())){
            criteria.andEqualTo(JobSchedule::getUsername,jobScheduleQuery.getUsername());
        }

        example.setOrderByClause("id desc");
        PageResultSet<JobSchedule> pageResultSet = new PageResultSet<>();
        pageResultSet.setRows(jobScheduleMapper.selectByExample(example));
        pageResultSet.setTotal(jobScheduleMapper.selectCountByExample(example));
        return pageResultSet;
    }
}
